package com.jamal2362.apktool.preference;

import android.content.Context;
import android.preference.PreferenceActivity;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.util.MUtil;
import com.jamal2362.apktool.util.Settings;

public class CustomHeaderAdapter extends ArrayAdapter<PreferenceActivity.Header>
{
	private Context mContext;
	
    private static class HeaderViewHolder
	{
        ImageView icon;
        TextView title;
        TextView summary;
    }

    private LayoutInflater mInflater;
    private int mLayoutResId;
    private boolean mRemoveIconIfEmpty;

    public CustomHeaderAdapter(Context context, List<PreferenceActivity.Header> objects, int layoutResId, boolean removeIconBehavior)
	{
        super(context, 0, objects);
		this.mContext = context;
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mLayoutResId = layoutResId;
        mRemoveIconIfEmpty = removeIconBehavior;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
	{
        HeaderViewHolder holder;
        View view;
        if (convertView == null)
		{
            view = mInflater.inflate(mLayoutResId, parent, false);
            holder = new HeaderViewHolder();
            holder.icon = view.findViewById(R.id.icon);
            holder.title = view.findViewById(R.id.title);
            holder.summary = view.findViewById(R.id.summary);
            view.setTag(holder);
        }
		else
		{
            view = convertView;
            holder = (HeaderViewHolder) view.getTag();
        }
		int dft = Settings.appFontSize - 4;
		int minh = MUtil.spAsPx(getContext(), (Settings.appFontSize * 2 + dft));
		view.setMinimumHeight(minh);
		holder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Settings.appFontSize);
		holder.summary.setTextSize(TypedValue.COMPLEX_UNIT_SP, dft);
        PreferenceActivity.Header header = getItem(position);
        if (mRemoveIconIfEmpty)
		{
            if (header.iconRes == 0)
			{
                holder.icon.setVisibility(View.GONE);
            }
			else
			{
				boolean lt = Settings.getb(mContext, "light_theme", true);
				int icon;
				switch(header.iconRes)
				{
					case R.drawable.ic_rebuild_light:
						icon = (lt ? header.iconRes : R.drawable.ic_rebuild_dark);
						break;
						
					case R.drawable.ic_edit_light:
						icon = (lt ? header.iconRes : R.drawable.ic_edit_dark);
						break;
						
					case R.drawable.ic_play_light:
						icon = (lt ? header.iconRes : R.drawable.ic_play_dark);
						break;
						
					case R.drawable.ic_sign_light:
						icon = (lt ? header.iconRes : R.drawable.ic_sign_dark);
						break;
						
					case R.drawable.ic_help_light:
						icon = (lt ? header.iconRes : R.drawable.ic_help_dark);
						break;
						
					default:
						icon = header.iconRes;
						break;
				}
                holder.icon.setVisibility(View.VISIBLE);
                holder.icon.setImageResource(icon);
            }
        }
		else
		{
            holder.icon.setImageResource(header.iconRes);
        }
        holder.title.setText(header.getTitle(getContext().getResources()));
        CharSequence summary = header.getSummary(getContext().getResources());
        if (!TextUtils.isEmpty(summary))
		{
            holder.summary.setVisibility(View.VISIBLE);
            holder.summary.setText(summary);
        }
		else
		{
            holder.summary.setVisibility(View.GONE);
        }
        return view;
    }
}
